package com.itcast.rpc.client.cluster.impl;

import com.itcast.common.utils.IpUtil;
import com.itcast.rpc.client.channel.ProviderService;
import com.itcast.rpc.client.cluster.ClusterStrategy;
import org.springframework.stereotype.Component;

import java.util.List;
@Component("hash")
public class HashClusterStrategy implements ClusterStrategy {
    @Override
    public ProviderService select(List<ProviderService> services) {
        String realIp = IpUtil.getRealIp();
        int hashCode = realIp.hashCode();
        int index = Math.abs(hashCode % services.size());
        return services.get(index);
    }
}
